import { message } from 'ant-design-vue'

// 错误类型枚举
export enum ErrorType {
  NETWORK = 'NETWORK',
  API = 'API',
  VALIDATION = 'VALIDATION',
  UNKNOWN = 'UNKNOWN'
}

// 错误信息接口
export interface ErrorInfo {
  type: ErrorType
  message: string
  code?: number
  details?: any
}

// 处理API错误
export const handleApiError = (error: any): ErrorInfo => {
  if (error.response) {
    // 服务器响应了错误状态码
    const { status, data } = error.response
    return {
      type: ErrorType.API,
      message: data?.info || `请求失败 (${status})`,
      code: status,
      details: data
    }
  } else if (error.request) {
    // 请求已发出但没有收到响应
    return {
      type: ErrorType.NETWORK,
      message: '网络连接失败，请检查网络设置',
      details: error.request
    }
  } else {
    // 其他错误
    return {
      type: ErrorType.UNKNOWN,
      message: error.message || '未知错误',
      details: error
    }
  }
}

// 显示错误消息
export const showError = (error: ErrorInfo | string): void => {
  const messageText = typeof error === 'string' ? error : error.message
  message.error(messageText)
}

// 显示成功消息
export const showSuccess = (msg: string): void => {
  message.success(msg)
}

// 显示警告消息
export const showWarning = (msg: string): void => {
  message.warning(msg)
}

// 显示信息消息
export const showInfo = (msg: string): void => {
  message.info(msg)
}
